Skip to content

perf: enable NAX Metal dispatch — 2.3x prefill speedup - #2

Merged
ekryski merged 1 commit into
ekryski:ek/speed-improvements-2from
TheTom:ek/speed-improvements-2
Apr 14, 2026
Merged

ekryski merged 1 commit into
ekryski:ek/speed-improvements-2from
TheTom:ek/speed-improvements-2

Conversation

@TheTom

@TheTom TheTom commented Apr 14, 2026

Copy link
Copy Markdown

Summary

  • Remove MLX_METAL_NO_NAX flag — enables non-atomic Metal command buffer execution
  • Guard NAX SDPA dispatch for BD<=128 only (BD=256 NAXTile has zero-length array JIT bug)
  • 2.3x prefill throughput: 8K → 19K tok/s on Gemma 4 E2B at 512 context (M5 Max)

Test plan

  • Gemma 4 E2B prefill benchmark: 128–4096 context sweep
  • Qwen3 MoE prefill (head_dim=128, should benefit from NAX)
  • Decode regression check (NAX should not affect decode path)

🤖 Generated with Claude Code

TheTom added a commit to TheTom/mlx-swift-lm that referenced this pull request Apr 14, 2026
Switch Gemma prefill bridge from SPM static linking to dlopen/dlsym.
SPM's link-time optimizer miscompiles quantized_matmul → matmul,
causing shape errors. dlopen prevents cross-module optimization.

- Gemma4.swift: dlopen bridge with function pointer dispatch
- build-prefill-bridge.sh: compile bridge dylib with clang++ -O3
  using -undefined dynamic_lookup (shares host MLX allocator)
- benchmark.sh: add --bridge flag to pass NATIVE_PREFILL=1

With NAX enabled in mlx-swift (ekryski/mlx-swift#2):
  512 tok: 19,583 tok/s (was 8,342)
  1024 tok: 18,780 tok/s (was 8,683)

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@TheTom
TheTom force-pushed the ek/speed-improvements-2 branch from 80f7034 to 0dfbc2c Compare April 14, 2026 21:24
Remove MLX_METAL_NO_NAX flag. NAX (Non-Atomic eXecution) allows Metal
command buffers to execute without serialization barriers, giving 2.3x
prefill throughput on Gemma 4 E2B (8K → 19K tok/s at 512 context).

Guard NAX SDPA for BD<=128 only — BD=256 NAXTile has zero-length array
bug that crashes JIT compilation.

Co-Authored-By: tturney1@gmail.com
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@TheTom
TheTom force-pushed the ek/speed-improvements-2 branch from 0dfbc2c to 923e61d Compare April 14, 2026 21:24
TheTom added a commit to TheTom/mlx-swift-lm that referenced this pull request Apr 14, 2026
Switch Gemma prefill bridge from SPM static linking to dlopen/dlsym.
SPM's link-time optimizer miscompiles quantized_matmul → matmul,
causing shape errors. dlopen prevents cross-module optimization.

- Gemma4.swift: dlopen bridge with function pointer dispatch
- build-prefill-bridge.sh: compile bridge dylib with clang++ -O3
  using -undefined dynamic_lookup (shares host MLX allocator)
- benchmark.sh: add --bridge flag to pass NATIVE_PREFILL=1

With NAX enabled in mlx-swift (ekryski/mlx-swift#2):
  512 tok: 19,583 tok/s (was 8,342)
  1024 tok: 18,780 tok/s (was 8,683)

Co-Authored-By: tturney1@gmail.com
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
TheTom added a commit to TheTom/mlx-swift-lm that referenced this pull request Apr 14, 2026
Switch Gemma prefill bridge from SPM static linking to dlopen/dlsym.
SPM's link-time optimizer miscompiles quantized_matmul → matmul,
causing shape errors. dlopen prevents cross-module optimization.

- Gemma4.swift: dlopen bridge with function pointer dispatch
- build-prefill-bridge.sh: compile bridge dylib with clang++ -O3
  using -undefined dynamic_lookup (shares host MLX allocator)
- benchmark.sh: add --bridge flag to pass NATIVE_PREFILL=1

With NAX enabled in mlx-swift (ekryski/mlx-swift#2):
  512 tok: 19,583 tok/s (was 8,342)
  1024 tok: 18,780 tok/s (was 8,683)

Co-Authored-By: tturney1@gmail.com
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
TheTom added a commit to TheTom/mlx-swift-lm that referenced this pull request Apr 14, 2026
Switch Gemma prefill bridge from SPM static linking to dlopen/dlsym.
SPM's link-time optimizer miscompiles quantized_matmul → matmul,
causing shape errors. dlopen prevents cross-module optimization.

- Gemma4.swift: dlopen bridge with function pointer dispatch
- build-prefill-bridge.sh: compile bridge dylib with clang++ -O3
  using -undefined dynamic_lookup (shares host MLX allocator)
- benchmark.sh: add --bridge flag to pass NATIVE_PREFILL=1

With NAX enabled in mlx-swift (ekryski/mlx-swift#2):
  512 tok: 19,583 tok/s (was 8,342)
  1024 tok: 18,780 tok/s (was 8,683)

Co-Authored-By: tturney1@gmail.com
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ekryski
ekryski merged commit 1d11c3d into ekryski:ek/speed-improvements-2 Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants